import api from '@api'
const state = {
  list: {
    data: [],
    pageNum: 0,
    page: 1
  },
  userIntegral: 0
}

const getters = {
  ['getJifen'] (state) {
    return state.list
  },
  ['getAll'] (state) {
    return state.userIntegral
  }
}

const actions = {
  async ['getJifen'] ({commit, state}, page) {
    if(page == 1) {
      commit('clearJifen');
    }
    let config = {
      CurrentPage: page || 1,
      PageSize: 20,
    };
    let {data: {userIntegral, integralList, pageNum, StatusCode}} = await api.get('api/v1/UserInfo/userIntegralList.ashx', config);
    commit('addJifen', {list: integralList, pageNum: Number(pageNum), page});
    commit('updataJifen', userIntegral)
    return StatusCode
  }
}

const mutations = {
  ['addJifen'] (state, {list, pageNum, page}) {
    state.list.data = state.list.data.concat(list);
    state.list.pageNum = pageNum;
    state.list.page = page;
  },
  ['updataJifen'] (state, jifen) {
    state.userIntegral = jifen
  },
  ['clearJifen'] (state) {
    state.list = {
        data: [],
        pageNum: 0,
        page: 1
      }
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
